function getAllWare() {
    var sel = document.getElementById("dcName"); //发货仓添加
    $.ajax({
        url: 'https://'+ window.location.host +'/api/admin/abaseservice/area/getAllWarehouse',
        cache: false, //禁用缓存
        type: "POST",
        dataType:"json",
        data:{},
        success: function(obj){
            for(var i=0;i<obj.data.length;i++){
                var option = new Option(obj.data[i].warehouse_name,obj.data[i].warehouse_code);
                sel.options.add(option)
            }
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            alert(XMLHttpRequest);
        }
    });
}

function dateValue(type) {
    if(type ==1) {
        if(parseInt(document.getElementById('dayNumber').value)<parseInt(document.getElementById('timeNumber').value)){
            $(document.getElementById('dayNumber')).val("");
        }
    }else{
        if(parseInt(document.getElementById('dayNumber').value)<parseInt(document.getElementById('timeNumber').value)){
            $(document.getElementById('timeNumber')).val("");
        }
    }
}


function addOrderTime() { //添加下单时段

    $("#OrderTimeList").append('<li class="clearfix pl-10 mb-5">\n' +
        '<div class="f-l pd-5">\n' +
        '<input type="radio" class="radio"/><label class="pl-10">00:00—12:00</label>\n' +
        '</div><div class="f-r lh-30"><a href="##" class="Hui-iconfont icon-edit mr-10">&#xe600;</a>\n' +
        '<a href="##" class="Hui-iconfont icon-edit mr-10">&#xe6df;</a>\n' +
        '<a href="##" class="Hui-iconfont icon-edit mr-10">&#xe6e2;</a>' +
        '<input type="text" class="beginInput" hidden><input class="endInput" type="text" hidden>' +
        '<input class="noonInput" type="text" hidden></div></li>');
    
}

function delOrderTime(thil,type) {
    if(type == 1){
        $(thil).parent().parent().remove();
    }else{
        console.log($(thil).parent().parent().remove())
        $(thil).parent().parent().remove();
        inputAllTime();
    }

}

var type = 1;
var thlsupdate;
function insertTime(thls) { //添加 可选时段
    if(document.getElementById('beginTime').value == "" && document.getElementById('endTime').value == ""){
        layer.msg('不能为空', {icon: 5,time:1000});
        return;
    }
    var beginTime = $("#beginTime").val();
    var endTime = $("#endTime").val();
    var nonnId = $('#noon option:selected').val();
    var nonnName = $('#noon option:selected').text();
    if (beginTime == "" || endTime == "") {
        layer.msg('不能为空', {icon: 5, time: 1000});
        return;
    }
    if (beginTime == 0) {
        beginTime = "00";
    }
    if (endTime == 0) {
        endTime = "00";
    }
    var ulList = document.getElementById('optionalList');
    var liList = $(ulList).children();
    for(var i=0;i<liList.length;i++){
        var allspan = $(liList[i]).find('.time').children();
        if(allspan[0].id == beginTime && allspan[1].id == endTime){
            layer.msg('时段重复', {icon: 5,time:1000});
            return;
        }
    }
    if (type ==1) {
        $("#optionalList").append('<li class="clearfix pl-10 mb-5">\n' +
            '<div class="f-l pd-5">\n' +
            '<input type="radio" class="radio"/><label class="pl-10 time"><span id="'+beginTime+'">' + beginTime + '</span>' +":00"+ '—<span id="'+endTime+'">' + endTime + '</span>' +":00"+ '<span id="' + nonnId + '" >' + nonnName + '</span></label>\n' +
            '</div>\n' +
            '<div class="f-r lh-30">\n' +
            '<a href="##" class="Hui-iconfont icon-edit mr-10" onclick="updateTime(this)">&#xe6df;</a>\n' +
            '<a href="##" class="Hui-iconfont icon-edit mr-10" onclick="delOrderTime(this)">&#xe6e2;</a>\n' +
            '</div></li>')
    }else{
        var spanupdate = $(thlsupdate).parent().prev().find('.time').children();
        spanupdate[0].id = beginTime;
        $(spanupdate[0]).text(beginTime);
        spanupdate[1].id = endTime;
        $(spanupdate[1]).text(endTime);
        spanupdate[2].id = nonnId;
        $(spanupdate[2]).text(nonnName);
        type = 1;

    }
    $("#beginTime").val('')
    $("#endTime").val('')
    inputAllTime();
}

function inputAllTime() { //给每个下单时段的配送时段汇总
    var ulList = document.getElementById('optionalList');
    var liList = $(ulList).children();
    var beginInput = $(updateThis).nextAll()[2];
    var endInput = $(updateThis).nextAll()[3];
    var noonInput = $(updateThis).nextAll()[4];
    var beginValue = "";
    var endValue = "";
    var noonValue = "";

    for(var j=0;j<liList.length;j++){
        var allspan = $(liList[j]).find('.time').children();
        beginValue += allspan[0].id+",";
        endValue += allspan[1].id+",";
        noonValue += allspan[2].id+",";
    }
    $(beginInput).val(beginValue);
    $(endInput).val(endValue);
    $(noonInput).val(noonValue);
}



function moreThan(id) {
        if(parseInt(document.getElementById('beginTime').value) > 24){
            $(document.getElementById('beginTime')).val(24);
        }

        if(parseInt(document.getElementById('endTime').value) > 24){
            $(document.getElementById('endTime')).val(24);
        }
        if(id == 1){
            if(parseInt(document.getElementById('beginTime').value) > parseInt(document.getElementById('endTime').value)){
                layer.msg('不能小于', {icon: 5,time:1000});
                $(document.getElementById('beginTime')).val("");
            }
            if(parseInt(document.getElementById('beginTime').value) == parseInt(document.getElementById('endTime').value)){
                layer.msg('不能重复', {icon: 5,time:1000});
                $(document.getElementById('beginTime')).val("");
            }
        }else{
            if(parseInt(document.getElementById('beginTime').value) > parseInt(document.getElementById('endTime').value)){
                layer.msg('不能小于', {icon: 5,time:1000});
                $(document.getElementById('endTime')).val("");
            }
            if(parseInt(document.getElementById('beginTime').value) == parseInt(document.getElementById('endTime').value)){
                layer.msg('不能重复', {icon: 5,time:1000});
                $(document.getElementById('endTime')).val("");
            }
        }

        if(parseInt(document.getElementById('beginTime').value) >=0 && parseInt(document.getElementById('beginTime').value)  < 12){
            var noonList = document.getElementById("noon");
            $(noonList).html("");
            var option = new Option("上午","0");
            noonList.options.add(option);
        }else if(parseInt(document.getElementById('beginTime').value) >= 12 && parseInt(document.getElementById('beginTime').value) < 17){
            var noonList = document.getElementById("noon");
            $(noonList).html("");
            var option = new Option("下午","1");
            noonList.options.add(option);

        }else if(parseInt(document.getElementById('beginTime').value) >= 17 && parseInt(document.getElementById('beginTime').value) <= 24){
            var noonList = document.getElementById("noon");
            $(noonList).html("");
            var option = new Option("晚上","2");
            noonList.options.add(option);
        }

}
function updateTime(thils) { //修改物流配送时间

    var time = $(thils).parent().prev().find('.time').children();
    $("#beginTime").val($(time[0]).text());
    $("#endTime").val($(time[1]).text());
    var noonList = document.getElementById("noon");
    if (time[2].id == 0){
        $(noonList).html("");
        var option = new Option("上午","0");
        noonList.options.add(option);
    }
    if (time[2].id == 1){
        $(noonList).html("");
        var option = new Option("下午","1");
        noonList.options.add(option);
    }
    if (time[2].id == 2){
        $(noonList).html("");
        var option = new Option("晚上","2");
        noonList.options.add(option);
    }
    type=2;
    thlsupdate = thils;
}


var updateThis;

function updOrderTime(thir) { //修改下单对应的物流配送时间

    updateThis = thir;
    var beginInput = $($(updateThis).nextAll()[2]).val();
    var endInput = $($(updateThis).nextAll()[3]).val();
    var noonInput = $($(updateThis).nextAll()[4]).val();

    if (beginInput == "" || beginInput == null) {
        $("#optionalList").html('');
    } else {
        $("#optionalList").html('')
        var beginInputList = beginInput.split(",")
        var endInputList = endInput.split(",")
        var noonInputList = noonInput.split(",")
        beginInputList.splice(beginInputList.length - 1, 1);
        endInputList.splice(endInputList.length - 1, 1);
        noonInputList.splice(noonInputList.length - 1, 1);
        var noonNameList = noonInputList;
        for (var i = 0; i < beginInputList.length; i++) {
            if (noonNameList[i] == 1) {
                noonNameList[i] = '下午'
            }
            if (noonNameList[i] == 0) {
                noonNameList[i] = '上午'
            }
            if (noonNameList[i] == 2) {
                noonNameList[i] = '晚上'
            }
            $("#optionalList").append('<li class="clearfix pl-10 mb-5">\n' +
                '<div class="f-l pd-5">\n' +
                '<input type="radio" class="radio"/><label class="pl-10 time"><span id="' + beginInputList[i] + '">' + beginInputList[i] + '</span>' + ":00" + '—<span id="' + endInputList[i] + '">' + endInputList[i] + '</span>' + ":00" + '<span id="' + noonInputList[i] + '" >' + noonNameList[i] + '</span></label>\n' +
                '</div>\n' +
                '<div class="f-r lh-30">\n' +
                '<a href="##" class="Hui-iconfont icon-edit mr-10" onclick="updateTime(this)">&#xe6df;</a>\n' +
                '<a href="##" class="Hui-iconfont icon-edit mr-10" onclick="delOrderTime(this)">&#xe6e2;</a>\n' +
                '</div></li>')
        }

    }

    $("#optionalTime").show()
}

function cleanTime() { //点击取消清理input框
    $("#beginTime").val();
    $("#endTime").val();
}